﻿using SupplyChainWeb.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;

namespace SupplyChainWeb.Service
{

   
    /// <summary>
    /// OrderService 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消注释以下行。 
    // [System.Web.Script.Services.ScriptService]
    public class OrderService : System.Web.Services.WebService
    {
        private static log4net.ILog log = log4net.LogManager.GetLogger("OrderService");
        //public CookieContainer CookieContainer;
        public UserValidationSoapHeader userValidation;
        [SoapHeader("userValidation")]
        [WebMethod(Description = "登录方法，返回值用来指名是不是登录成功，并且这个值在之后的接口中用来找到相对应的服务器上的session",
          EnableSession = true)]
        public string HelloLogin()
        {
            if (userValidation == null || (userValidation != null && (string.IsNullOrEmpty(userValidation.UserName) || string.IsNullOrEmpty(userValidation.Password))))
            {
                return "用户名及密码不能为空 ";
            }
            if (userValidation != null && E10LoginService.e10Lgoin2(userValidation.UserName, userValidation.Password, this.Session))
            {
                if (this.Session["LoginModel"] != null)
                {
                    LoginModel _LoginModel = (LoginModel)this.Session["LoginModel"];

                    return "登入成功";
                }
                else
                {
                    return "登入失败";
                }
                //return "login in ";
            }
            //用户名或密码不正确

            return "Hello World";
        }



        [SoapHeader("userValidation")]
        [WebMethod(Description = "获取供应商订单信息",
          EnableSession = true)]
        public DataSet getVendorOrder(String VendorID)
        {
            DataSet ds = new DataSet();
            if (userValidation == null || (userValidation != null && (string.IsNullOrEmpty(userValidation.UserName) || string.IsNullOrEmpty(userValidation.Password))))
            {
                return null;
            }
            if (userValidation != null && E10LoginService.e10Lgoin2(userValidation.UserName, userValidation.Password, this.Session))
            {
                if (this.Session["LoginModel"] != null)
                {
                    LoginModel _LoginModel = (LoginModel)this.Session["LoginModel"];

                }
                else
                {
                }
            }
            if (this.Session["LoginModel"] != null)
            {
                LoginModel _LoginModel = (LoginModel)this.Session["LoginModel"];
                var orderquery = SCMDataProvider.GetOrderRel(VendorID).Cast<OrderRel>();
                //DataTable dt = orderquery.ToList<OrderRel>().ToTable();//SCMDataProvider.ToTable(orderquery);
                List<OrderRel> list = orderquery.ToList<OrderRel>();
                DataTable dt = list.ToDataTable<OrderRel>(a => new object[] { list });
                dt.TableName = "Order";
                ds.Tables.Add(dt);
            }
            else
            {
                return null;
            }

            return ds;
        }

        [WebMethod(Description = "获取供应商订单信息",
           EnableSession = true)]
        public DataSet getVendorOrder2(String VendorID)
        {
            DataSet ds = new DataSet();
            var orderquery = SCMDataProvider.GetOrderRel(VendorID).Cast<OrderRel>();
            List<OrderRel> list = orderquery.ToList<OrderRel>();
            DataTable dt = list.ToDataTable<OrderRel>(a => new object[] { list }) ;
            dt.TableName = "Order";
            ds.Tables.Add(dt);
            return ds;
        }


        //<item Text = "发货申请确认(中礼)" NavigateUrl="/Home/IndexShipCfm" /><!--finsh=true ReqShip=true ShipCfm=false-->
        /// <summary>
        /// 获取发货申请确认(中礼)
        /// </summary>
        /// <param name="VendorID"></param>
        /// <returns></returns>
        [SoapHeader("userValidation")]
        [WebMethod(Description = "获取发货申请确认(中礼)",
          EnableSession = true)]
        public DataSet getVendorOrderShipCfm(String VendorID)
        {
            DataSet ds = new DataSet();
            if (userValidation == null || (userValidation != null && (string.IsNullOrEmpty(userValidation.UserName) || string.IsNullOrEmpty(userValidation.Password))))
            {
                return null;
            }
            if (userValidation != null && E10LoginService.e10Lgoin2(userValidation.UserName, userValidation.Password, this.Session))
            {
                if (this.Session["LoginModel"] != null)
                {
                    LoginModel _LoginModel = (LoginModel)this.Session["LoginModel"];

                }
                else
                {
                }
            }
            if (this.Session["LoginModel"] != null)
            {
                LoginModel _LoginModel = (LoginModel)this.Session["LoginModel"];
                var orderquery = SCMDataProvider.GetOrderShipCfm(VendorID).Cast<OrderRel>();
                //DataTable dt = orderquery.ToList<OrderRel>().ToTable();//SCMDataProvider.ToTable(orderquery);
                List<OrderRel> list = orderquery.ToList<OrderRel>();
                DataTable dt = list.ToDataTable<OrderRel>(a => new object[] { list });
                dt.TableName = "Order";
                ds.Tables.Add(dt);
            }
            else
            {
                return null;
            }

            return ds;
        }

        /// <summary>
        /// 更新发货申请确认(中礼)
        /// </summary>
        /// <param name="VendorID"></param>
        /// <returns></returns>
        [SoapHeader("userValidation")]
        [WebMethod(Description = "更新发货申请确认(中礼)",
          EnableSession = true)]
        public bool updateVendorOrderShipCfm(String VendorID,int OrderNum,int OrderLine,int OrderRelNum)
        {
            bool success = false;
            if (userValidation == null || (userValidation != null && (string.IsNullOrEmpty(userValidation.UserName) || string.IsNullOrEmpty(userValidation.Password))))
            {
                return false;
            }
            if (userValidation != null && E10LoginService.e10Lgoin2(userValidation.UserName, userValidation.Password, this.Session))
            {
                if (this.Session["LoginModel"] != null)
                {
                    LoginModel _LoginModel = (LoginModel)this.Session["LoginModel"];

                }
                else
                {
                }
            }
            if (this.Session["LoginModel"] != null)
            {
                LoginModel _LoginModel = (LoginModel)this.Session["LoginModel"];
                if (SCMDataProvider.GetintByOrderShip(VendorID, OrderNum, OrderLine) > 0)
                {
                    //更新
                    OrderShip os = SCMDataProvider.DB.OrderShip.First(o => o.Company == VendorID && o.OrderNum == OrderNum 
                        && o.OrderLine == OrderLine && o.OrderRelNum == OrderRelNum);
                    if (os.ReqShip == true)//dtl.ShipCfm != null &&
                    {

                        os.ReqShip = true;
                        os.ShipCfm = true;
                        os.UserID5 = _LoginModel.UserID;
                        os.ShipCfmDate = DateTime.Now;


                    }
                    SCMDataProvider.DB.SubmitChanges();
                }
            }
                return success;
        }

        //<item Text = "确认收货(中礼)" NavigateUrl="/Home/IndexShipped" /><!--finsh=true ReqShip=true ShipCfm=true Ship=true Shipped=false-->
        /// <summary>
        /// 获取确认收货(中礼)
        /// </summary>
        /// <param name="VendorID"></param>
        /// <returns></returns>
        [SoapHeader("userValidation")]
        [WebMethod(Description = "获取确认收货(中礼)",
          EnableSession = true)]
        public DataSet getVendorOrderShipped(String VendorID)
        {
            DataSet ds = new DataSet();
            if (userValidation == null || (userValidation != null && (string.IsNullOrEmpty(userValidation.UserName) || string.IsNullOrEmpty(userValidation.Password))))
            {
                return null;
            }
            if (userValidation != null && E10LoginService.e10Lgoin2(userValidation.UserName, userValidation.Password, this.Session))
            {
                if (this.Session["LoginModel"] != null)
                {
                    LoginModel _LoginModel = (LoginModel)this.Session["LoginModel"];

                }
                else
                {
                }
            }
            if (this.Session["LoginModel"] != null)
            {
                LoginModel _LoginModel = (LoginModel)this.Session["LoginModel"];
                var orderquery = SCMDataProvider.GetOrderShipped(VendorID).Cast<OrderRel>();
                //DataTable dt = orderquery.ToList<OrderRel>().ToTable();//SCMDataProvider.ToTable(orderquery);
                List<OrderRel> list = orderquery.ToList<OrderRel>();
                DataTable dt = list.ToDataTable<OrderRel>(a => new object[] { list });
                dt.TableName = "Order";
                ds.Tables.Add(dt);
            }
            else
            {
                return null;
            }

            return ds;
        }
        /// <summary>
        /// 更新确认收货(中礼)
        /// </summary>
        /// <param name="VendorID"></param>
        /// <returns></returns>
        [SoapHeader("userValidation")]
        [WebMethod(Description = "更新确认收货(中礼)",
          EnableSession = true)]
        public bool updateVendorOrderShipped(String VendorID, int OrderNum, int OrderLine, int OrderRelNum,decimal RcvQty)
        {
            bool success = false;
            if (userValidation == null || (userValidation != null && (string.IsNullOrEmpty(userValidation.UserName) || string.IsNullOrEmpty(userValidation.Password))))
            {
                return false;
            }
            if (userValidation != null && E10LoginService.e10Lgoin2(userValidation.UserName, userValidation.Password, this.Session))
            {
                if (this.Session["LoginModel"] != null)
                {
                    LoginModel _LoginModel = (LoginModel)this.Session["LoginModel"];

                }
                else
                {
                }
            }
            if (this.Session["LoginModel"] != null)
            {
                LoginModel _LoginModel = (LoginModel)this.Session["LoginModel"];

                if (SCMDataProvider.GetintByOrderShip(VendorID,  OrderNum,  OrderLine) > 0)
                {
                    //更新
                    OrderShip os = SCMDataProvider.DB.OrderShip.First(o => o.Company ==  VendorID && o.OrderNum ==  OrderNum 
                    && o.OrderLine ==  OrderLine && o.OrderRelNum ==  OrderRelNum);
                    //更新收货数量rcvqty   收货数量0  全部不合格
                    if ( RcvQty < 0  || RcvQty > os.ReqQty)
                    {
                        os.RcvQty =  os.ReqQty;
                    }
                    else
                    {
                        os.RcvQty =  RcvQty;
                    }

                    //if (os.RcvConfirm == true)//dtl.ShipCfm != null &&
                    {

                        os.RcvConfirm = true;
                        os.UserID7 = _LoginModel.UserID;
                        os.RcvDate = DateTime.Now;


                    }
                    SCMDataProvider.DB.SubmitChanges();
                }
            }
            return success;
        }
        //<item Text = "确认对账(中礼)" NavigateUrl="/Home/IndexInv" /><!--Shipped=true ReqInvoice=true Invoice=false-->
        /// <summary>
        /// 获取确认对账(中礼)
        /// </summary>
        /// <param name="VendorID"></param>
        /// <returns></returns>
        [SoapHeader("userValidation")]
        [WebMethod(Description = "获取确认对账(中礼)",
          EnableSession = true)]
        public DataSet getVendorOrderInv(String VendorID)
        {
            DataSet ds = new DataSet();
            if (userValidation == null || (userValidation != null && (string.IsNullOrEmpty(userValidation.UserName) || string.IsNullOrEmpty(userValidation.Password))))
            {
                return null;
            }
            if (userValidation != null && E10LoginService.e10Lgoin2(userValidation.UserName, userValidation.Password, this.Session))
            {
                if (this.Session["LoginModel"] != null)
                {
                    LoginModel _LoginModel = (LoginModel)this.Session["LoginModel"];

                }
                else
                {
                }
            }
            if (this.Session["LoginModel"] != null)
            {
                LoginModel _LoginModel = (LoginModel)this.Session["LoginModel"];
                var orderquery = SCMDataProvider.GetOrderInv(VendorID).Cast<OrderRel>();
                //DataTable dt = orderquery.ToList<OrderRel>().ToTable();//SCMDataProvider.ToTable(orderquery);
                List<OrderRel> list = orderquery.ToList<OrderRel>();
                DataTable dt = list.ToDataTable<OrderRel>(a => new object[] { list });
                dt.TableName = "Order";
                ds.Tables.Add(dt);
            }
            else
            {
                return null;
            }

            return ds;
        }

        /// <summary>
        /// 更新确认对账(中礼)
        /// </summary>
        /// <param name="VendorID"></param>
        /// <returns></returns>
        [SoapHeader("userValidation")]
        [WebMethod(Description = "更新确认对账(中礼)",
          EnableSession = true)]
        public bool updateVendorOrderInv(String VendorID, int OrderNum, int OrderLine, int OrderRelNum)
        {
            bool success = false;
            if (userValidation == null || (userValidation != null && (string.IsNullOrEmpty(userValidation.UserName) || string.IsNullOrEmpty(userValidation.Password))))
            {
                return false;
            }
            if (userValidation != null && E10LoginService.e10Lgoin2(userValidation.UserName, userValidation.Password, this.Session))
            {
                if (this.Session["LoginModel"] != null)
                {
                    LoginModel _LoginModel = (LoginModel)this.Session["LoginModel"];

                }
                else
                {
                }
            }
            if (this.Session["LoginModel"] != null)
            {
                LoginModel _LoginModel = (LoginModel)this.Session["LoginModel"];
                if (SCMDataProvider.GetintByOrderShip(VendorID, OrderNum, OrderLine) > 0)
                {
                    //更新
                    if (SCMDataProvider.GetintByOrderShip(VendorID,  OrderNum,  OrderLine) > 0)
                    {
                        //更新
                        OrderShip os = SCMDataProvider.DB.OrderShip.First(o => o.Company == VendorID && o.OrderNum ==OrderNum 
                            && o.OrderLine == OrderLine && o.OrderRelNum == OrderRelNum);

                        //if (dtl.Invoice == true)//dtl.ShipCfm != null &&
                        {

                            os.Invoice = true;
                            os.UserID9 = _LoginModel.UserID;
                            os.InvoiceDate = DateTime.Now;

                        }
                        SCMDataProvider.DB.SubmitChanges();
                    }
                }
            }
            return success;
        }

        /// <summary>
        /// 更新供应商订单上的采购单号
        /// </summary>
        /// <param name="VendorID"></param>
        /// <returns></returns>
        [SoapHeader("userValidation")]
        [WebMethod(Description = "更新供应商订单上的采购单号",
          EnableSession = true)]
        public bool updatePONum(int OrderNum, int OrderLine, int PONum, int POLine)
        {
            bool success = false;
            if (userValidation == null || (userValidation != null && (string.IsNullOrEmpty(userValidation.UserName) || string.IsNullOrEmpty(userValidation.Password))))
            {
                return false;
            }
            if (userValidation != null && E10LoginService.e10Lgoin2(userValidation.UserName, userValidation.Password, this.Session))
            {
                if (this.Session["LoginModel"] != null)
                {
                    LoginModel _LoginModel = (LoginModel)this.Session["LoginModel"];

                }
                else
                {
                }
            }
            if (this.Session["LoginModel"] != null)
            {
                LoginModel _LoginModel = (LoginModel)this.Session["LoginModel"];
                //update Erp.OrderHed set PONum = 1 where OrderNum = 1
                //update Erp.OrderDtl set POLine = 1 where OrderNum = 1 and OrderLine = 1

                string sql_a = string.Format("update Erp.OrderHed set PONum = {0} where OrderNum = {1}",PONum,OrderNum);
                string sql_b = string.Format("update Erp.OrderDtl set POLine = {0} where OrderNum = {1} and OrderLine = {2}",POLine,OrderNum,OrderLine);
                if (SQLHelper.ExecuteNonQuery(SQLHelper.ConnectionString,CommandType.Text,sql_a) > 0
                    && SQLHelper.ExecuteNonQuery(SQLHelper.ConnectionString, CommandType.Text, sql_b) > 0)
                {
                    success = true;
                }
            }
            return success;
        }


    }
}
